package com.tobby.camelinaction;

import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;

/**
 * This is the aggregation strategy which is java code for <i>aggregating</i> incoming messages with the existing
 * aggregated message. In other words you use this strategy to <i>merge</i> the message together.
 * 
 * @author ll00252
 * @version $Revision$
 */
public class MyAggregationStrategy implements AggregationStrategy {

	/**
	 * Aggregates the messages.
	 * 
	 * @param oldExchange the existing aggregated message. Is <tt>null</tt> the very first time as there are no existing
	 * message.
	 * @param newExchange the incoming message. This is never <tt>null<tt>.
	 * @return the aggregated message.
	 */
	@Override
	public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
		// the first time there are no existing message and therefore the oldExchage is null. In these cases we just
		// return the new Exchange
		if (oldExchange == null) {
			return newExchange;
		}

		// now we have both an existing message (oldExchange) and an incoming message (newExchange) we want to merge
		// together.

		// in this example we add their bodies
		String oldBody = oldExchange.getIn().getBody(String.class);
		String newBody = newExchange.getIn().getBody(String.class);

		// the body should be the two bodies added together
		String body = oldBody + newBody;

		// update the existing message with added body.
		oldExchange.getIn().setBody(body);

		// and return it
		return oldExchange;
	}

}
